import axios, { AxiosInstance } from "axios";
import { RootState } from "../actions/store";

function getRequest(state: RootState): AxiosInstance {
  if (!state.auth.loggedIn) {
    throw new Error();
  }
  const result = axios.create({
    baseURL: 'http://localhost:8080/'
  });
  result.interceptors.request.use((config) => {
    config.headers.Authorization = `Bearer ${state.auth.token}`;
    return config;
  });
  return result;
}

export { getRequest };